﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AMS.DataAccess.Sql
{
    /// <summary>
    /// 将多个SQL件表达式用连接符“AND”或"OR"连接形成新的表达式，并用括号()将结果括起来。如：
    /// (AID = 1
    ///      OR Name LIKE 'XIAOMING%'
    ///      OR Sex = 2)
    /// </summary>
    /// <example>
    /// SqlConditionGroup group = new SqlConditionGroup("AND");
    /// </example>
    public class SqlConditionGroup : SqlPiece
    {
        /// <summary>
        /// 表达式组，使用默认的连接操作符 AND
        /// </summary>
        public SqlConditionGroup()
        {
            conditions = new List<string>();
        }

        /// <summary>
        /// 表达式组
        /// </summary>
        /// <param name="logicOperation">连接符</param>
        public SqlConditionGroup(LogicOperations logicOperation)
            : this()
        {
            operation = logicOperation;
        }

        /// <summary>
        /// 表达式组
        /// </summary>
        /// <param name="logicOperation">连接符</param>
        public SqlConditionGroup(string logicOperation)
            : this()
        {
            this.strOperation = logicOperation;
            conditions = new List<string>();
        }

        /// <summary>
        /// 表达式连接操作符，默认为 AND
        /// </summary>
        private LogicOperations operation = LogicOperations.AND;

        /// <summary>
        /// 操作符
        /// </summary>
        private string strOperation;

        /// <summary>
        /// 条件列表
        /// </summary>
        private List<string> conditions;

        /// <summary>
        /// 添加一个条件
        /// </summary>
        /// <param name="condition">条件</param>
        /// <returns>this</returns>
        public SqlConditionGroup Add(SqlCondition condition)
        {
            conditions.Add(condition.ToString());
            return this;
        }

        /// <summary>
        /// 添加一个条件
        /// </summary>
        /// <param name="condition">条件</param>
        /// <returns>this</returns>
        public SqlConditionGroup Add(string condition)
        {
            conditions.Add(condition);
            return this;
        }

        /// <summary>
        /// 将内容转换成SQL语句
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (conditions.Count == 0)
            {
                return string.Empty;
            }

            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("(");
            string sOp = strOperation;
            if (string.IsNullOrEmpty(sOp))
            {
                sOp = CompactMode ? " AND " : Environment.NewLine + "     AND ";
                if (operation == LogicOperations.OR)
                {
                    sOp = CompactMode ? " OR " : Environment.NewLine + "     OR ";
                }
            }

            sbSql.Append(string.Join(sOp, conditions.ToArray()));
            sbSql.Append(")");
            return sbSql.ToString();
        }
    }
}
